package com.pzj.dormitory.controller;


import com.pzj.dormitory.entity.DormitoryAdmin;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.service.DormitoryAdminService;
import com.pzj.dormitory.service.impl.DormitoryAdminServiceImpl;
import com.pzj.dormitory.util.ResultVOUtil;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.ResultVO;
import com.pzj.dormitory.form.RuleForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/dormitoryAdmin")
public class DormitoryAdminController {

    @Autowired
    private DormitoryAdminService dormitoryService;



    @GetMapping("/login")
    public ResultVO login(RuleForm ruleForm){
        return dormitoryService.login(ruleForm);
    }

    @PostMapping("/save")
    public ResultVO save(@RequestBody DormitoryAdmin dormitoryAdmin){
        boolean save = dormitoryService.save(dormitoryAdmin);
        if(!save) return ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVO<DormitoryAdmin> list(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageVO list = dormitoryService.list(page, size);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/search")
    public ResultVO<DormitoryAdmin> search(SearchForm searchForm){
        System.out.println(searchForm);
        System.out.println(dormitoryService.search(searchForm));
        return ResultVOUtil.success(dormitoryService.search(searchForm));
    }

    @GetMapping("findById/{id}")
    public ResultVO findById(@PathVariable("id") Integer id){
        return ResultVOUtil.success(dormitoryService.getById(id));
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody DormitoryAdmin dormitoryAdmin){
        System.out.println("dormitoryAdmin = " + dormitoryAdmin);
        return ResultVOUtil.success(dormitoryService.updateById(dormitoryAdmin));
    }

    @DeleteMapping("/delById/{id}")
    public ResultVO delById(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        return ResultVOUtil.success(dormitoryService.removeById(id));
    }

    @GetMapping("/findAll")
    public ResultVO findAll(){
        List<DormitoryAdmin> list = dormitoryService.list();
        return  ResultVOUtil.success(list);
    }
}

